//
//  Mo-Base.swift
//  soapp
//
//  Created by zhu xietong on 2017/8/11.
//  Copyright © 2017年 CocoaPods. All rights reserved.
//

import Foundation
import UIKit


@objc open class MoAtrribute:NSObject{
    weak var ower:MoAtrributeProtocol?

    
    weak var _controller:UIViewController? = nil
    @objc public var controller:UIViewController?{
        set{
            if let ctr = newValue
            {
                _controller = ctr
                if let node = ower as? ASDisplayNode{
                    node.mo_controller = _controller
                    node.mo_theme = ctr.mo_theme
                }
                if let view = ower as? UIView{
                    view.mo_controller = _controller
                    view.mo_theme = ctr.mo_theme

                }
                ower?.mo_set_controller(controller: ctr)
            }
        }
        get{
            if let ctr = _controller{
                return ctr
            }
            else{
                if let v = self.ower as? ControllerSubKitSupport{
                    return v.getSelfContainerController()
                }
            
                return nil
            }
        }
    }
    
    public var needReload:Bool = true
    //第一数据
    @objc public var model = NSMutableDictionary(){
        willSet{
            ower?.mo_model_willSet(new: newValue)
        }
        didSet{
            if needReload
            {
                ower?.mo_model_reload()
            }
        }
    }
    //第二数据
    @objc public var detail = NSMutableDictionary(){
        willSet{
            ower?.mo_detail_willSet(new: newValue)
        }
        didSet{
            ower?.mo_detail_reload()
        }
    }
    
    
}


public protocol MoAtrributeProtocol:NSObjectProtocol {
    var mo:MoAtrribute{get set}
    
    func mo_model_willSet(new:NSMutableDictionary)
    func mo_model_reload()
    func mo_model_relase(current:NSMutableDictionary)
    
    func mo_detail_willSet(new:NSMutableDictionary)
    func mo_detail_reload()
    func mo_detail_relase(current:NSMutableDictionary)
    
    func mo_set_controller(controller:UIViewController)
    func mo_change_localizable(obj:Any?)
    
}



extension MoAtrributeProtocol where Self: UIViewController
{
    
}


struct MoAtrributeProtocolKeys {
    static var mo = "MoAtrributeProtocolKeys_mo"
}



extension  MoAtrributeProtocol where Self:UIView
{
    public func mo_change_localizable(obj:Any?){
        
    }

    
    public func mo_model_willSet(new: NSMutableDictionary) {
        
    }
    
    public func mo_model_reload() {
        
    }
    
    public func mo_model_relase(current: NSMutableDictionary) {
        
    }
        
    public func mo_detail_willSet(new: NSMutableDictionary) {
        
    }
    
    public func mo_detail_reload() {
        
    }
    
    public func mo_detail_relase(current: NSMutableDictionary) {
        
    }
    
    
    
    public var mo:MoAtrribute{
        get{
            if let obj = objc_getAssociatedObject(self, &MoAtrributeProtocolKeys.mo) as? MoAtrribute
            {
                return obj
            }
            let newValue = MoAtrribute()
            
            newValue.ower = self
            objc_setAssociatedObject(self, &MoAtrributeProtocolKeys.mo, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            return newValue
        }
        set{
            self.mo_model_willSet(new: newValue.model)
            self.mo_detail_willSet(new: newValue.detail)
            
            if let obj = objc_getAssociatedObject(self, &MoAtrributeProtocolKeys.mo) as? MoAtrribute
            {
                self.mo_model_relase(current: obj.model)
                self.mo_detail_relase(current: obj.detail)
            }
            
            objc_setAssociatedObject(self, &MoAtrributeProtocolKeys.mo, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            newValue.ower = self
            self.mo_model_reload()
            
        }
    }
    
}





extension MoAtrributeProtocol where Self:UIViewController
{
    public func mo_model_willSet(new: NSMutableDictionary) {
        
    }
    
    public func mo_model_reload() {
        
    }
    
    public func mo_model_relase(current: NSMutableDictionary) {
    }
    
    
    
    public func mo_detail_willSet(new: NSMutableDictionary) {
        
    }
    
    public func mo_detail_reload() {
        
    }
    
    public func mo_detail_relase(current: NSMutableDictionary) {
        
    }
    
    public var mo:MoAtrribute{
        get{
            if let obj = objc_getAssociatedObject(self, &MoAtrributeProtocolKeys.mo) as? MoAtrribute
            {
                return obj
            }
            let newValue = MoAtrribute()
            
            newValue.ower = self
            objc_setAssociatedObject(self, &MoAtrributeProtocolKeys.mo, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            return newValue
        }
        set{
            
            
            self.mo_model_willSet(new: newValue.model)
            self.mo_detail_willSet(new: newValue.detail)
            
            if let obj = objc_getAssociatedObject(self, &MoAtrributeProtocolKeys.mo) as? MoAtrribute
            {
                self.mo_model_relase(current: obj.model)
                self.mo_detail_relase(current: obj.detail)
            }
            
            objc_setAssociatedObject(self, &MoAtrributeProtocolKeys.mo, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            newValue.ower = self
            self.mo_model_reload()
            self.mo_detail_reload()
        }
    }
}


public protocol ControllerSubKitSupport
{
    func getSelfContainerController() -> UIViewController?
}

extension UIView:ControllerSubKitSupport{
    
    private struct Owner_controllerKey {
        static var Owner_controllerKey = "Owner_controllerKey"
    }

     public var _mo_owner_controller:UIViewController?{
        get{
            if let obj = objc_getAssociatedObject(self, &Owner_controllerKey.Owner_controllerKey) as? UIViewController
            {
                return obj
            }
            return nil
        }
        set{
            objc_setAssociatedObject(self, &Owner_controllerKey.Owner_controllerKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_ASSIGN)
        }
    }
    
    public func getSelfContainerController() -> UIViewController?{
        if let ctr = _mo_owner_controller
        {
            return ctr
        }else{
            if let sv = self.superview{
                return sv.getSelfContainerController()
            }
            else{
                return nil
            }
        }
    }
    
}

